# coding:utf-8


def inspect(connection):
    cursor = connection.cursor()
    cursor.execute('select name from sqlite_master where type=\'table\';')
    tables = tuple(x[0] for x in cursor.fetchall()
                        if not x[0].startswith('sqlite_'))
    fields = {}
    for name in tables:
        cursor.execute('PRAGMA table_info(\'' + name + '\')')
        fields[name] = []
        for field in cursor.fetchall():
            fields[name].append(dict(name=field[1], type=field[2],
            length=0, not_null=field[3], default=field[4], pk=field[5]))
    dd = {}
    for t in tables:
        f = dict((f['name'], f) for f in fields[t])
        dd[t] = f
    return dd